home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 297_01 / prcnsult.c < prev    next >
C/C++ Source or Header  |  1980-01-01  |  5KB  |  218 lines

  1. /* prcnsult.c */
  2. /* load a file of clauses */
  3.  
  4. #include <stdio.h>
  5. #include "prtypes.h"
  6.  
  7.  
  8. #define CANTLOAD "Can't load %s"
  9. #define ERRBUILTIN "Can't redefine builtin %s"
  10. #define HEADNOTATOM "Predicate not atom!"
  11.  
  12. extern char *Print_buffer;
  13. extern varindx Nvars;
  14. extern FILE *Curr_infile;
  15. extern atom_ptr_t LastBuiltin;
  16. extern int Trace_flag;
  17.  
  18. void add_to_end();
  19. void record_pred();
  20. void do_listing();
  21.  
  22. unsigned int Inp_linecount;
  23. pred_rec_ptr_t First_pred, Last_pred;
  24. static int Pred_count = 0;
  25. static char Filename[80];
  26.  
  27. /******************************************************************
  28.             make_clause()
  29.  ******************************************************************/
  30. clause_ptr_t make_clause(clhead, clgoals, status, predptr)
  31. node_ptr_t clhead, clgoals;
  32. atom_ptr_t *predptr;
  33. {
  34.     clause_ptr_t clauseptr, get_clause();
  35.     ENTER("make_clause");
  36.  
  37.     clauseptr = get_clause(status);
  38.     CLAUSEPTR_GOALS(clauseptr) = clgoals;
  39.     CLAUSEPTR_HEAD(clauseptr) = clhead;
  40.     CLAUSEPTR_NVARS(clauseptr) = Nvars * sizeof(struct subst);
  41.     CLAUSEPTR_NEXT(clauseptr) = NULL;
  42.         if(NODEPTR_TYPE(clhead) == PAIR)
  43.         {
  44.  
  45.             clhead = NODEPTR_HEAD(clhead);
  46.             if(NODEPTR_TYPE(clhead) != ATOM)
  47.               {
  48.               errmsg(HEADNOTATOM);
  49.               return(NULL);/* could also indicate which one !! */
  50.               }
  51.             *predptr = NODEPTR_ATOM(clhead);
  52.         }
  53.     return(clauseptr);
  54. }
  55.  
  56. /******************************************************************
  57.             do_consult()
  58.  ******************************************************************/
  59.  
  60. do_consult(infilename,  status)
  61. char *infilename;
  62. int status;
  63. {
  64.     extern node_ptr_t NilNodeptr;
  65.     extern FILE *Curr_outfile;
  66.  
  67.     FILE *ifp, *save_cif,  *save_cof;
  68.     atom_ptr_t the_pred;
  69.     clause_ptr_t clauseptr;
  70.     node_ptr_t the_list, the_head, read_list();
  71.  
  72.     ENTER("do_consult");
  73.     Inp_linecount = 0;
  74.  
  75.     if((ifp = fopen(infilename, "r")) == NULL)
  76.     {
  77.         sprintf(Print_buffer, CANTLOAD, infilename);
  78.         errmsg(Print_buffer);
  79.         return(0);
  80.     }
  81.     else
  82.         save_cif = Curr_infile;
  83.     strcpy(Filename, infilename);
  84.     Curr_infile = ifp;
  85.     save_cof = Curr_outfile;
  86.     Curr_outfile = stdout;
  87.     while((the_list = read_list(status)) != NULL)
  88.     {
  89.         the_head = NODEPTR_HEAD(the_list);
  90.         if(NODEPTR_TYPE(the_head) == ATOM)
  91.         {
  92.             clauseptr = make_clause(the_list, NilNodeptr, status,
  93.                 &the_pred);
  94.         }
  95.         else
  96.             clauseptr = make_clause(the_head, NODEPTR_TAIL(the_list), status, &the_pred);
  97.         if(!clauseptr)
  98.             {
  99.             fclose(Curr_infile);
  100.             return(0);
  101.             }
  102.         if(Trace_flag > 0){
  103.             pr_string("adding \n");
  104.             pr_clause(clauseptr);
  105.             pr_string("\n");
  106.         }
  107.         add_to_end(clauseptr, the_pred);
  108.     }
  109.     fclose(Curr_infile);
  110.     Curr_infile = save_cif;
  111.     Curr_outfile = save_cof;
  112.     return(1);
  113. }
  114.  
  115. /******************************************************************************
  116.              load()
  117.  The usual function used to load a file.
  118.  Called in prmain.c
  119.  ******************************************************************************/
  120. load(s)
  121. char *s;
  122. {
  123. ENTER("load");
  124. return(do_consult(s, PERMANENT));
  125. }
  126.  
  127. /*********************************************************************
  128.         add_to_end()
  129. Adds a clause to the end of its packet.
  130. ********************************************************************/
  131. void add_to_end(clauseptr, pred)
  132. clause_ptr_t clauseptr;
  133. atom_ptr_t pred;
  134. {
  135.     clause_ptr_t clp, previous;
  136.  
  137.     ENTER("add_to_end");
  138.     clp = ATOMPTR_CLAUSE(pred);
  139.     record_pred(pred);
  140.  
  141.     if(clp == NULL)
  142.     {
  143.         ATOMPTR_CLAUSE(pred) = clauseptr;
  144.     }
  145.     else
  146.     {
  147.         previous = clp;
  148.  
  149.         while(clp != NULL)
  150.         {
  151.             previous = clp;
  152.             clp = CLAUSEPTR_NEXT(clp);
  153.         }
  154.  
  155.         CLAUSEPTR_NEXT(previous) = clauseptr;
  156.     }
  157. }
  158.  
  159. /**********************************************************************
  160.             record_pred() 
  161. Record the atom pointer as a predicate and verify we are not
  162. redefining a primitive.
  163. **********************************************************************/
  164. void record_pred(atomptr)
  165. atom_ptr_t atomptr;
  166. {
  167.     extern unsigned int Inp_linecount;
  168.     pred_rec_ptr_t predptr, get_pred();
  169.  
  170.     ENTER("record_pred");
  171.     if(atomptr < LastBuiltin)
  172.     {
  173.         sprintf(Print_buffer, "%s line %d ", Filename, Inp_linecount);
  174.         errmsg(Print_buffer);
  175.     
  176.         fatal2("redefining builtin",  ATOMPTR_NAME(atomptr));
  177.     }
  178.  
  179.     if((atomptr < LastBuiltin) ||
  180.         (atomptr > LastBuiltin && ATOMPTR_CLAUSE(atomptr) == NULL))
  181.     {
  182.         predptr = get_pred();
  183.         predptr->atom = atomptr;
  184.  
  185.         if( Pred_count == 0)
  186.         {
  187.             First_pred = predptr;
  188.         }
  189.         else
  190.         {
  191.             Last_pred->next_pred = predptr;
  192.         }
  193.         Last_pred = predptr;
  194.         Pred_count++;
  195.     }
  196. }
  197.  
  198. /**********************************************************************
  199.         do_listing()
  200. List all clauses on current output file.
  201. **********************************************************************/
  202. void do_listing()
  203. {
  204.     pred_rec_ptr_t predptr;
  205.  
  206.     ENTER("do_listing");
  207.     for(predptr = First_pred; predptr != NULL; predptr = predptr->next_pred)
  208.     {
  209.         if(predptr->atom > LastBuiltin)
  210.         {
  211.             pr_packet(ATOMPTR_CLAUSE(predptr->atom));
  212.             pr_string("\n");
  213.         }
  214.     }
  215. }
  216.  
  217. /* end of file */
  218.